var flag = false;
var network = require("../../../utils/network.js");
Page({
  data:{
    icons:{
      map1:"http://housecleanimags-1255598775.cosbj.myqcloud.com/icon/map1.png",
      right:"http://housecleanimags-1255598775.cosbj.myqcloud.com/icon/right.png",
      time:"http://housecleanimags-1255598775.cosbj.myqcloud.com/icon/time.png",
      master:"http://housecleanimags-1255598775.cosbj.myqcloud.com/icon/master.png"    
    },
    defaultAddre:"",
    newcheckTimeList:"",
    hours:"",
    money:"",
    date:"",
    time:"",
    hidden:true,
    display1: "flex",
    display2: "none",
    comment:"",
    flag:true
  },
    // 跳转选择时间页面
  toSelectTime:function(){
    wx.navigateTo({
      url: '../../public/select-time/index',
    })
  },
   // 跳转选择地址页面
  toChooseAddre:function(){
    wx.navigateTo({
       url: '../../public/selectAddress/index'
    });
  },
   // 跳转选择我的帮手页面
  toMaster:function(){
    wx.navigateTo({
      url: '../../public/master/index'
    });
  },
  //点击立即预约
  submit: function() {
    var that = this 
    var warn = "";//弹框时提示的内容
    var flag = true;//判断信息输入是否完整
    var defaultAddre=this.data.defaultAddre
    var newcheckTimeList=this.data.newcheckTimeList
    if (!defaultAddre){
      warn = "请输入您的地址信息";
    } else if (!newcheckTimeList){
      warn = "请选择预约日期和时间";
    }else{   
      var data ={}
      var timestamp = (new Date()).valueOf()
      console.log(timestamp)
      data.addrId = that.data.defaultAddre
      data.price = that.data.curPrice
      data.serviceTimes =wx.getStorageSync("newcheckTimeList")
      data.openid = wx.getStorageSync("openid")
      data.uid = wx.getStorageSync("uid")
      data.token = wx.getStorageSync("token")
      data.duration = wx.getStorageSync("duration")*60
      network.postData(
        "https://api.hsb1234.com/userc/v1/order",
        data,
        function(res){
          var orderId = res.data.data.orderId
          that.setData({
            hidden:false,
            orderId: orderId
          })
        },
        function(){}
      )
       flag = false;
    }
   //如果信息填写不完整，弹出输入框
    if(flag==true){
      wx.showModal({
      title: '提示',
      content:warn
       })
    }
  },
  pay:function(){
    var that = this 
    var data = {}
    data.openid = wx.getStorageSync("openid")
    data.uid = wx.getStorageSync("uid")
    data.token = wx.getStorageSync("token")
    data.orderId = that.data.orderId
    network.postData(
      "https://api.hsb1234.com/userc/v1/pay",
      data,
      function(res){
        var resdata= res.data.data
        console.log(resdata)
        wx.requestPayment({
          'timeStamp': resdata.timeStamp,
          'nonceStr': resdata.nonceStr,
          'package': resdata.package,
          'signType': resdata.signType,
          'paySign': resdata.sign,
          'success': function (res) {
            wx.clearStorageSync("master")
            wx.clearStorageSync("newcheckTimeList")
            wx.clearStorageSync("duration")
            wx.showModal({
              title: '成功',
              content: '成功',
            })
            wx.redirectTo({
              url: '../../myMember/myOrder/index',
            })
          },
          'fail': function (res) {
            wx.showModal({
              title: '失败',
              content: '失败',
            })
          }
        })
      },
      function(){
      }
    )
  },
  cancel:function(){
    this.setData({
      hidden: true
    })
  },

  onLoad:function(){
    var that = this 
    var data ={}
    data.openid = wx.getStorageSync("openid")
    data.uid = wx.getStorageSync("uid")
    data.token = wx.getStorageSync("token")
    network.getData(
      "https://api.hsb1234.com/userc/v1/mine/addresses/default",
      data,
      function(res){
        var defaultAddre = res.data.data 
        if (defaultAddre){
          that.setData({
            defaultAddre: defaultAddre,
            display1:"none",
            display2:"flex"
          })
        }else{
          that.setData({
            display1: "flex",
            display2: "none"
          })
        }
      },
    )
    var data1 = {}
    data1.openid = wx.getStorageSync("openid")
    data1.duration = wx.getStorageSync("duration")*60
    that.setData({
      duration: wx.getStorageSync("duration")
    })
    data1.usersId = wx.getStorageSync("usersId")
    network.postData(
      "https://api.hsb1234.com/userc/v1/calculation/price",
      data1,
      function (res) {
        var resdata = res.data.data
        console.log(resdata)
        var curPrice = resdata.curPrice
        that.setData({
          curPrice: curPrice,
        })
      },
      function () {
      }
    )
  },
  onShareAppMessage: function () {
    return {
      title: "活速帮提供优质家政服务，让您畅享舒适生活",
      desc: "",
      path: "../../index/index",
      imageUrl:"http://housecleanimags-1255598775.file.myqcloud.com/screen/swiper1.jpg",
      success: function () {
        wx.showToast({
          title: '成功',
          icon: 'success',
          duration: 500
        })
      },
      fail: function () {
        wx.showToast({
          title: "谢谢体验",
          icon: 'fail',
          duration: 500
        })
      }
    }
  },
  onShow: function () {
    var newcheckTimeList = wx.getStorageSync("newcheckTimeList")
    if (newcheckTimeList) {
      this.setData({
        newcheckTimeList: newcheckTimeList,
        display3: "none",
        display4: "flex"
      })
    } else {
      this.setData({
        display3: "flex",
        display4: "none"
      })
    }
    var master = wx.getStorageSync("master")
    if (master) {
      this.setData({
        master: master,
        display5: "none",
        display6: "flex"
      })
    } else {
      this.setData({
        display5: "flex",
        display6: "none"
      })
    }
  },
})